package com.example.animations.controls;

import android.content.Context;
import android.graphics.ColorFilter;
import android.graphics.PorterDuff;
import android.util.AttributeSet;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.example.animations.R;

public class SliderView extends LinearLayout implements SeekBar.OnSeekBarChangeListener {
    private final float step = 0.1f;
    private float _minValue = 0.2f;
    private float _maxValue = 2.0f;
    private float _value = 1.0f;
    private String _title = "";

    private TextView _titleTextView;
    private SeekBar _seekBar;

    private IOnSliderValueChanged _onSliderValueChanged;

    public SliderView(Context context) {
        super(context);
        initialize();
    }

    public SliderView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initialize();
    }

    public SliderView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initialize();
    }

    private void initialize() {
        inflate(getContext(), R.layout.view_slider, this);
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();

        ((TextView) findViewById(R.id.min_value_text)).setText(String.valueOf(_minValue));
        ((TextView) findViewById(R.id.max_value_text)).setText(String.valueOf(_maxValue));

        _titleTextView = findViewById(R.id.title_view);
        _seekBar = findViewById(R.id.seek_bar);
        _seekBar.setOnSeekBarChangeListener(this);
        initSeekBarValues();
    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
        if (b) {
            _value = Math.round((_minValue + i * step) * 100.0f) / 100.0f;
            updateTitleValue();
        }
        if (_onSliderValueChanged != null) {
            _onSliderValueChanged.OnValueChanged(_value);
        }
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {

    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
    }

    private void initSeekBarValues() {
        int maximum = Math.round((_maxValue - _minValue) / step);

        _seekBar.setMin(0);
        _seekBar.setMax(maximum);
        _seekBar.setProgress(Math.round((_value - _minValue) / step));
        updateTitleValue();
    }

    private void updateTitleValue() {
        _titleTextView.setText(String.format("%s (%s)", _title, _value));
    }

    public void setTitle(String title) {
        _title = title;
    }

    public void setMinMaxValues(float minValue, float maxValue) {
        _minValue = minValue;
        _maxValue = maxValue;
    }

    public void setValue(float value) {
        _value = value;
    }

    public void onValueChanged(IOnSliderValueChanged onSliderValueChanged) {
        _onSliderValueChanged = onSliderValueChanged;
    }

    public interface IOnSliderValueChanged {
        void OnValueChanged(float value);
    }
}
